The point amount of the way from a to b - 0 gives a, 1 gives b, 0.5 the midpoint. Smoothing a camera or a displayed value toward its real target a little every frame is the commonest use.
lerp(a, b, amount) -> real| Name | Type |
|---|---|
a | real |
b | real |
amount | real |
real
The point amount of the way from a to b - 0 gives a, 1 gives b, 0.5 the exact midpoint. Short for "linear interpolation": a straight-line blend between two values, not a curve.
The commonest use in a 2D game is smoothing - moving a displayed value a little closer to its real target every frame, instead of snapping straight to it, so the change reads as motion rather than a jump.
x = lerp(x, xstart, 0.1);
y = lerp(y, ystart, 0.1);Drifts this object back toward wherever it spawned, a tenth of the remaining distance closer every frame - a gentle spring-back rather than a sudden snap. Because the remaining distance keeps shrinking, so does the step taken each frame, which is what gives this its natural "settling" feel with no separate slow-down logic needed.
A value that is smoothed toward a target does not creep for ever: once the answer is heading for b and gets within about 0.0156 (1/64) of it, lerp just hands you b itself. So x = lerp(x, target, 0.5) run every frame ends with x exactly equal to target, and then stays there. You can safely write if x == target after it, or stop doing work once they match. (Before this, the value could stall a hair short of the target - 99.99 instead of 100 - and never quite arrive.)
This only ever happens in that last sliver. lerp(0, 100, 0.5) is still 50, lerp(a, b, 0) is still exactly a, and an amount outside 0-1 still goes past the ends (see below) - a result that has overshot b is never pulled back to it.
amount is coarser than it lookslerp multiplies b - a by amount, and a multiply of two real numbers first cuts both down to steps of 1/64 (about 0.0156). So 0.1 does not blend by a tenth: it is only 6 of those steps, which is 0.09375. lerp(0, 100, 0.1) gives 9.375, not 10. For the smoothing above this does not matter, since the target is only ever approached by degrees (and the last sliver is finished for you, see above). An amount smaller than 1/64 (about 0.0156, so 0.01) is too small to count: it rounds down to nothing and the value does not move (beyond that last sliver). When the exact fraction does matter, pick an amount that is a whole number of 64ths (0.5, 0.25, 0.125), or skip lerp and write a + (b - a) / 10, because dividing by a whole number is exact. Numbers explains where the 1/64 comes from.
amount is never clamped hereGiving 0.5 returns the midpoint and giving 0 or 1 returns a or b exactly, but anything outside 0-1 is not stopped at either end - it keeps extrapolating in the same direction (lerp(0, 10, 2) is 20, past b; lerp(0, 10, -1) is -10, past a). This is the more useful default: a caller that wants the clamped kind already has it, one call away - lerp(a, b, clamp(amount, 0, 1)) - but there is no way to go the other direction and recover extrapolation from a version that always saturated.
One multiply and one add - no divide, cheap enough for a value smoothed every single frame.
One multiply, one add and a couple of compares - no divide. amount is never clamped to 0-1 here: anything outside that range keeps extrapolating past a or b, the more generally useful default (wrap the amount in clamp(amount, 0, 1) for the clamped kind - there is no way to go the other direction and un-clamp one that always saturated). It finishes: when the answer is heading for b and lands within 1/64 of it, you get b exactly, so x = lerp(x, target, 0.5) every frame reaches target and then stays there instead of creeping toward it for ever. An amount under 1/64 (about 0.0156) is too small to move anything at all.